using UnityEditor;
using UnityEngine;

namespace FShaderInspector
{
    /// <summary>
    /// 折页标签
    /// </summary>
    public class FoldoutDrawer : FMaterialPropertyDrawer
    {
        /// <summary> 默认折页状态 </summary>
        readonly bool _defaultOpen;

        public FoldoutDrawer() : this(1) { }

        public FoldoutDrawer(float level, float open = 0)
        {
            this.level = (int)level;
            _defaultOpen = (int)open >= 1;
        }

        public override void OnGUI(Rect position, MaterialProperty prop, GUIContent label, MaterialEditor editor)
        {
            base.OnGUI(position, prop, label, editor);

            // 如果level等级 > ShaderGUI等级 && 不是折叠状态 && 是子集的情况下不执行
            if (level > FShaderGUI.level && !FShaderGUI.isOpen && FShaderGUI.IsChild())
                return;

            var hasMixedValue = HasMixedFoldout();

            var btnRect = FShaderGUI.GetTempRect();
            if (GUI.Button(new Rect(btnRect.x, btnRect.y, btnRect.width - 50, btnRect.height), "", EditorStyles.boldLabel))
            {
                if (!hasMixedValue)
                {
                    foreach (var target in prop.targets)
                    {
                        var key = EditorCache.instance.GetPropertyKey(target, prop.name);
                        var state = EditorCache.instance.GetKey(key);
                        EditorCache.instance.SetKey(key, !state);
                    }
                }
                else
                {
                    // 不同值状态 默认点击打开
                    foreach (var target in prop.targets)
                    {
                        var key = EditorCache.instance.GetPropertyKey(target, prop.name);
                        var state = !_defaultOpen;
                        EditorCache.instance.SetKey(key, state);
                    }
                }

                // 按住alt按键 展开折叠所有子选项
                if (Event.current.alt)
                {
                    FShaderGUI.SetFoldedChild(prop.name, level, _defaultOpen ? !EditorCache.instance.GetKey(labelKey) : EditorCache.instance.GetKey(labelKey));
                }
            }

            // 默认打开则反转 icon 状态
            label = GetFoldoutIcon(_defaultOpen, hasMixedValue);
            label.text = Localization.FindName(prop.displayName);

            switch (level)
            {
                case <= 1:
                    FShaderGUI.DrawFoldoutGUI(btnRect, label, new Color(0.75f, 0.75f, 0.75f, 1), true);
                    break;
                case 2:
                    FShaderGUI.DrawFoldoutGUI(btnRect, label, new Color(0, 0, 0, 0.2f), false);
                    break;
                default:
                    FShaderGUI.DrawFoldoutGUI(btnRect, label, new Color(0, 0, 0, 0), false);
                    break;
            }

            // 传递当前页面 level
            FShaderGUI.level = level;

            if (hasMixedValue)
            {
                // 不同值状态 关闭展开
                FShaderGUI.isOpen = false;
            }
            else
            {
                // 如果默认开启 则反转更新状态到 Editor
                FShaderGUI.isOpen = _defaultOpen ? !EditorCache.instance.GetKey(labelKey) : EditorCache.instance.GetKey(labelKey);
            }
        }
    }
}